
package com.sxl.controller.admin;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sxl.controller.MyController;
import org.springframework.web.bind.annotation.RequestParam;

@Controller("productController")
@RequestMapping(value = "/admin/product")
public class ProductController extends MyController {


    //我的上传
    @RequestMapping(value = "/myFrame")
    public String myFrame(Model model, HttpServletRequest request) {
        Map param = new HashMap();
        return "/admin/product/myFrame";
    }

    //商品管理
    @RequestMapping(value = "/frame")
    public String frame(Model model, HttpServletRequest request) {
        return "/admin/product/frame";
    }

    //后台商品管理列表
    @RequestMapping(value = "/adminFrame")
    public String adminFrame(Model model, HttpServletRequest request) {
        return "/admin/product/adminFrame";
    }

    @RequestMapping(value = "/list")
    public String list(Model model, HttpServletRequest request, String productName) {
        String sql = "select a.*,(select typesName from t_types b where a.typesId=b.id) typesName  from t_product a";
        //过滤下架的商品
        sql += " where status='上架'";
        if (productName != null && !"".equals(productName)) {
            sql += " and productName like '%" + productName + "%'";
        }
        sql += " order by id desc";
        List list = db.queryForList(sql);
        request.setAttribute("list", list);
        return "/admin/product/list";
    }

    @RequestMapping(value = "/myList")
    public String listMy(Model model, HttpServletRequest request, String productName) {
        Map customerBean = (Map) request.getSession().getAttribute("customerBean");
        Integer customerId = (Integer) customerBean.get("id");
        String sql = "select a.*,(select typesName from t_types b where a.typesId=b.id) typesName  from t_product a where customerId=" + customerId;
        if (productName != null && !"".equals(productName)) {
            sql += " and productName like '%" + productName + "%'";
        }
        sql += " order by id desc";
        List list = db.queryForList(sql);
        request.setAttribute("list", list);
        return "/admin/product/myList";
    }

    @RequestMapping(value = "/adminList")
    public String adminList(Model model, HttpServletRequest request, String productName) {
        String sql = "select a.*,(select typesName from t_types b where a.typesId=b.id) typesName  from t_product a";
        if (productName != null && !"".equals(productName)) {
            sql += " where productName like '%" + productName + "%'";
        }
        sql += " order by id desc";
        List list = db.queryForList(sql);
        request.setAttribute("list", list);
        return "/admin/product/myList";
    }

    @RequestMapping(value = "/detial")
    public String detial(Model model, HttpServletRequest request, @RequestParam String productId) {
        String sql = "select a.*,(select typesName from t_types b where a.typesId=b.id) typesName  from t_product a";
        if (productId != null && !"".equals(productId)) {
            sql += " where productName id =" + productId + " limit 1";
        }
        List commentList = db.queryForList("select * from t_comment where productId=?", new Object[]{productId});
        Map result = db.queryForMap(sql);
        List list = new ArrayList();
        list.add(request);
        request.setAttribute("list", list);
        request.setAttribute("commentList", commentList);
        request.setAttribute("productId", productId);
        return "/admin/product/detail";
    }

    @RequestMapping(value = "/commentAdd")
    public ResponseEntity<String> saveComment(Model model,HttpServletRequest request, String productId, String content) {
        int result = 0;
        String sql="insert into t_comment(content,userName,userId,productId) values(?,?,?,?)";
        result = db.update(sql, new Object[]{content, getCustomer(request).get("username"), getCustomer(request).get("id"), productId});
        if(result==1){
            return renderData(true,"操作成功",null);
        }else{
            return renderData(false,"操作失败",null);
        }
    }

    @RequestMapping(value = "/editSave")
    public ResponseEntity<String> editSave(Model model, HttpServletRequest request, Long id, String flag
            , String productName, String productPic1, String productPic2, String productPic3, String productPic4,
                                           Integer price, Integer oldPrice, String content, Integer nums, String tjxj, String status, String sellStatus, Integer typesId, Integer jf) throws Exception {
        Map customerBean = (Map) request.getSession().getAttribute("customerBean");
        Integer customerId = (Integer) customerBean.get("id");
        int result = 0;
        if (id != null) {
            String sql = "update t_product set productName=?,productPic1=?,productPic2=?,productPic3=?,productPic4=?,price=?," +
                    "oldPrice=?,content=?,nums=?,tjxj=?,status=?,sellStatus=?,typesId=?,jf=?,customerId=? where id=?";
            result = db.update(sql, new Object[]{productName, productPic1, productPic2, productPic3, productPic4, price, oldPrice, content, nums, tjxj, status, sellStatus, typesId, jf, customerId, id});
        } else {
            String sql = "insert into t_product(productName,productPic1,productPic2,productPic3,productPic4,price,oldPrice,content,nums,tjxj,status,sellStatus,typesId,jf,customerId) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            result = db.update(sql, new Object[]{productName, productPic1, productPic2, productPic3, productPic4, price, oldPrice, content, nums, tjxj, status, sellStatus, typesId, jf, customerId});
        }
        if (result == 1) {
            return renderData(true, "操作成功", null);
        } else {
            return renderData(false, "操作失败", null);
        }
    }

    //专家给出鉴定意见
    @RequestMapping(value = "/expertEditSave")
    public ResponseEntity<String> expertEditSave(Model model, HttpServletRequest request, Long id, String comments) {
        Map customerBean = (Map) request.getSession().getAttribute("customerBean");
        Integer customerId = (Integer) customerBean.get("id");
        //检查是否是已经鉴定过的专家
        String queryExpert = "select * from t_expert where expertId=" + customerId;
        List queryForList = db.queryForList(queryExpert);
        if (queryForList.size() == 0) {
            return renderData(false, "您还没通过专家鉴定，没法鉴定宝物!", null);
        }
        int result = 0;
        if (id != null) {
            String sql = "update t_product set comments=? where id=?";
            result = db.update(sql, new Object[]{comments, id});
        }
        if (result == 1) {
            return renderData(true, "鉴定成功", null);
        } else {
            return renderData(false, "操作失败", null);
        }
    }

    @RequestMapping(value = "/editDelete")
    public ResponseEntity<String> editDelete(Model model, HttpServletRequest request, Long id, String flag) throws Exception {
        String sql = "delete from t_product where id=?";
        int result = db.update(sql, new Object[]{id});
        if (result == 1) {
            return renderData(true, "操作成功", null);
        } else {
            return renderData(false, "操作失败", null);
        }

    }

    @RequestMapping(value = "/orderOne")
    public ResponseEntity<String> orderOne(Model model, HttpServletRequest request, Long id) {
        Map customerBean = (Map) request.getSession().getAttribute("customerBean");
        if (customerBean==null){
            return renderData(false, "请先登录", null);
        }
        Integer customerId = (Integer) customerBean.get("id");
        //根据商品id，将商品查询出来
        String proSql = "select * from t_product where id=" + id;
        List queryForList = db.queryForList(proSql);
        Map productMap = (Map) queryForList.get(0);
        //不可以购买自己上传的商品
        Integer customerId2 = (int) productMap.get("customerId");
        if (customerId==customerId2){
            return renderData(false, "不可以购买自己上传的商品", null);
        }
        String productName = (String) productMap.get("productName");
        Object price = productMap.get("price");

        String uuid = UUID.randomUUID().toString();
        String orderNum = uuid.substring(0, 12);
        String status = "未完成";
        Date insertDate = new Date();
        String sql = "insert into t_order(customerId,productDetail,allPrice,status,orderNum,insertDate,productId) values(?,?,?,?,?,?,?)";
        int result = db.update(sql, new Object[]{customerId, productName, price, status, orderNum, insertDate, id});
        //更新商品已卖出
        String updatePro = "update t_product set sellStatus=? where id=?";
        result = db.update(updatePro, new Object[]{"已卖出", id});
        if (result == 1) {
            return renderData(true, "购买成功", null);
        } else {
            return renderData(false, "购买失败", null);
        }

    }

    @RequestMapping(value = "/edit")
    public String edit(Model model, HttpServletRequest request, Long id, String flag) {
        Map map = new HashMap();
        if (id != null) {
            //修改
            String sql = "select * from t_product where id=?";
            map = db.queryForMap(sql, new Object[]{id});
        } else {
            map.put("sellStatus", "未卖出");
        }
        model.addAttribute("map", map);
        String sql = "select * from t_types";
        model.addAttribute("typesList", db.queryForList(sql));
        return "/admin/product/edit";
    }

    //鉴定是否是专家
    @RequestMapping(value = "/expert")
    public ResponseEntity<String> expert(Model model, HttpServletRequest request, Long id) {
        Map customerBean = (Map) request.getSession().getAttribute("customerBean");
        if (customerBean==null){
            return renderData(false, "您还进行登录操作,请先登录！", null);
        }
        Integer customerId = (Integer) customerBean.get("id");
        String query="select * from t_expert where expertId="+customerId+" and status="+"'已认证'";
        List queryForList = db.queryForList(query);
        if (queryForList.size()>0){
            //如果专家鉴定的是自己的古玩，需要给出禁止
            String sql = "select * from t_product where id=?";
            Map map = db.queryForMap(sql, new Object[]{id});
            Integer customerId1 = (Integer) map.get("customerId");
            if (customerId1.equals(customerId)){
                return renderData(false, "不可以鉴定自己上传的古玩!", null);
            }
            return renderData(true, "已认证", null);
        }else {
            return renderData(false, "您还未进行专家认证，没法进行专家鉴定!", null);
        }
    }

    @RequestMapping(value = "/expertEdit")
    public String expertEdit(Model model, HttpServletRequest request, Long id, String flag) {
        if (id != null) {
            //修改
            String sql = "select * from t_product where id=?";
            Map map = db.queryForMap(sql, new Object[]{id});
            model.addAttribute("map", map);
            sql = "select * from t_types";
            model.addAttribute("typesList", db.queryForList(sql));
        }
        return "/admin/product/expertEdit";
    }
}
